package com.rampage.controller.user;

import com.rampage.controller.BaseController;
import com.rampage.controller.RandomCodeController;
import com.rampage.entity.Result;
import com.rampage.entity.user.User;
import com.rampage.service.user.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

@RequestMapping("/user")
@RestController
public class UserController extends BaseController<User, Integer> {

    @Autowired
    private UserService mUserService;

    @RequestMapping("doLogin")
    public Result doLogin(String username, String password, boolean rememberme, String verifyCode,
                          HttpServletRequest request, HttpServletResponse response, HttpSession session) {
        Object seesionVerifyCode = session.getAttribute(RandomCodeController.RANDOM_CODE_SESSION);
        if (seesionVerifyCode == null || !seesionVerifyCode.equals(verifyCode)) {
            return buildFaildResult("验证码不正确");
        }

        User user = mUserService.findUserByUsername(username);
        if (user == null || !user.getPassword().equals(password) || !user.isEnable()) {
            return buildFaildResult("用户名或密码错误");
        }

        return buildSuccessResult(user);

    }




}
